package com.cn.newetv.utils;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;

import com.cn.newetv.content.EtvData;

public class SysCfgDao {
	private ContentResolver contentResolver;
	
	public void openDB(Context context) {
		try {
			contentResolver = context.getContentResolver();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void insert(String key, String value) {
		try {
			ContentValues values = new ContentValues();
			values.put(EtvData.SystemConfigDO.PKEY, key);
			values.put(EtvData.SystemConfigDO.PVALUE, value);
			contentResolver.insert(EtvData.SystemConfigDO.WORD_CONTENT_URI, values);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void update(String key, String value) {
		try {
			ContentValues values = new ContentValues();
			values.put(EtvData.SystemConfigDO.PVALUE, value);
			String where = "paramkey=?";
			String[] selectionArgs = new String[]{key} ;
			contentResolver.update(EtvData.SystemConfigDO.WORD_CONTENT_URI, values, where, selectionArgs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void deleteByKey(String key) {
		try {
			String where = "paramkey=?";
			String[] selectionArgs = new String[]{key} ;
			contentResolver.delete(EtvData.SystemConfigDO.WORD_CONTENT_URI, where, selectionArgs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void deleteAll() {
		try {
			contentResolver.delete(EtvData.SystemConfigDO.WORD_CONTENT_URI, null, null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	private Cursor selectByKey(String key) {
		Cursor cursor = null;
	  	try {
			String selection = "paramkey=?";
			String[] selectionArgs = new String[]{key} ;
			String[] projection = new String[]{EtvData.SystemConfigDO.PKEY,EtvData.SystemConfigDO.PVALUE};
			cursor = contentResolver.query(EtvData.SystemConfigDO.DICT_CONTENT_URI, projection, selection, selectionArgs, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cursor;
	}
	
	private Cursor select() {
		Cursor cursor = null;
	  	try {
	  		String[] projection = new String[]{EtvData.SystemConfigDO.PKEY,EtvData.SystemConfigDO.PVALUE};
	  		cursor = contentResolver.query(EtvData.SystemConfigDO.DICT_CONTENT_URI, projection, null, null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cursor;
	}
	
	public Map<String, String> findAll() {
		Map<String, String> res = new HashMap<String, String>();
		try {
			Cursor cursor = select();
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				res.put(cursor.getString(0), cursor.getString(1));
				Log.d("SysCfgDao", cursor.getString(0)+"<===>"+cursor.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public void add(String key, String value) {
		try {
			insert(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void modify(String key, String value) {
		try {
			update(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getByKey(String key) {
		String value = null;
		try {
			Cursor cursor = selectByKey(key);
			if(cursor == null || cursor.getCount() == 0)
				return value;
			cursor.moveToNext();
			value = cursor.getString(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}
	
	public void removeByKey(String key) {
		try {
			deleteByKey(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void removeAll() {
		try {
			deleteAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
